//
//  RJWarningSectionView.m
//  AirQuality
//
//  Created by Po on 15/4/8.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarningSectionView.h"
@interface RJWarningSectionView ()

@property (retain, nonatomic) UIButton * currentButton;

- (void)initInterfaceWithTitles:(NSArray *)titles;
@end

@implementation RJWarningSectionView

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initInterfaceWithTitles:titles];
    }
    return self;
}

- (void)initInterfaceWithTitles:(NSArray *)titles
{
    CGFloat width = self.frame.size.width / titles.count;
    CGFloat height = self.frame.size.height;
    NSUInteger count = 0;
    UIView * boxView = [[UIView alloc]  initWithFrame:CGRectMake(0, 0, self.frame.size.width, height)];
    [boxView.layer setBorderColor:[UIColor whiteColor].CGColor];
    [boxView.layer setBorderWidth:1];
    [boxView.layer setCornerRadius:height/4];
    [self addSubview:boxView];
    
    for (NSString * title in titles) {
        UIButton * button = [self createButtonWithFrame:CGRectMake(1 + width * count, 1, width - 2 , height - 2) Title:title];
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:(count == 0 ? UIRectCornerBottomLeft | UIRectCornerTopLeft : UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(height / 4, height / 4)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = button.bounds;
        maskLayer.path = maskPath.CGPath;
        button.layer.mask = maskLayer;
        [self addSubview:button];
        if (count == 0) {
            [button setSelected:YES];
            [button setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"warningSectionBack"]]];
            _currentButton = button;
        }
        [button setTag:390 + count];
        count ++;
    }
}

- (UIButton *)createButtonWithFrame:(CGRect)frame Title:(NSString *)title
{
    UIButton * button = [[UIButton alloc] initWithFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [button setBackgroundColor:[UIColor clearColor]];
    [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}


#pragma mark - event
- (void)pressButton:(UIButton *)button
{
    if (_currentButton == button) {
        return;
    }
    
    if (_delegate) {
        if (![_delegate WarningSection:self didSelected:button.tag  - 390]){
            return;
        }
    }
    [_currentButton setSelected:NO];
    
    [_currentButton setBackgroundColor:[UIColor clearColor]];
    _currentButton = button;
    [_currentButton setSelected:YES];
    [_currentButton setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"warningSectionBack"]]];
}
@end
